package com.blogspot.radanddev.mbanksmsreader;

import android.content.BroadcastReceiver;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.gsm.SmsMessage;

/**
 * @author: Radoslaw Czwojdrak (Rad and Dev)
 */

public class smsReceiver extends BroadcastReceiver {
	
	@Override
	public void onReceive(Context context, Intent intent) {
        Bundle bundleObj = intent.getExtras();        
        SmsMessage[] msgs = null;
        String sms_text = "";   
        String operacja = "";
        
        if (bundleObj != null)
        {
            Object[] pdus = (Object[]) bundleObj.get("pdus");
            msgs = new SmsMessage[pdus.length];            
            msgs[0] = SmsMessage.createFromPdu((byte[])pdus[0]);
            
            if (msgs[0].getOriginatingAddress().compareTo("3388") == 0)
            {
            	sms_text = msgs[0].getMessageBody().toString();
            	
            	operacja = sms_text.substring(12, 14);
            	
            	sms_text = sms_text.substring(sms_text.length()-15, sms_text.length()-7);
            	sms_text = sms_text.substring(0, 4) + "  " + sms_text.substring(4, 8);

            	
                Intent startActivity = new Intent();
                startActivity.setClass(context, Okienko.class);
                startActivity.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
                startActivity.putExtra("operacja", operacja);
                startActivity.putExtra("haslo", sms_text);
                context.startActivity(startActivity);
            }
        }
	}
}